﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class ProOutNumMoneyChartController : BaseController<usersorder>
    {
        //
        // GET: /ProOutNumMoneyChart/
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }

        }
        public override BLL.BaseBll<usersorder> bll
        {
            get
            {
                return new UsersOrderBll();
            }
        }

        OrderDetailsBll orderDetailBll = new OrderDetailsBll();

        StoreBll storeBll = new StoreBll();

        WaresBll waresBll = new WaresBll();

        public class ListSaleDetail
        {
            public string shopName { get; set; }
            public string waresName { get; set; }           
            public decimal saleMoney { get; set; }   // 出售总金额
            public long saleNum { get; set; }   //出售数量
        }
        public class ListWare
        {
            public string waresCode { get; set; }
            public string waresName { get; set; }
            public long saleNum { get; set; }
            public int storeid { get; set; }
        }
        public class ListDetailall
        {
            public decimal orderSumprice { get; set; }
            public int? shopId { get; set; }
            public long saleNum { get; set; }
        }

        public ActionResult GetShopName()
        {
            return Json(storeBll.Search(x=>x.storeStatus==1).Select(x => new { shopID = x.storeid, shopName = x.storeName }).ToList());
        }
        /// <summary>
        /// 加载数据列表  -----KO
        ///  商品打折问题 需要解决  盈利金额的问题需要解决
        /// </summary>
        /// <param name="model"></param>
        /// <param name="waresName"></param>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public ActionResult GetAll(string waresName, string date1, string date2)
        {
            var listSale = new List<ListSaleDetail>();
            var listAlldetail = new List<ListDetailall>();
            var orderByList = new List<ListSaleDetail>();

            var listWare = new List<ListWare>();
            Expression<Func<usersorder, bool>> condition = x => x.orderStatus == 1;
            string dat2a = DateTime.Now.ToLongDateString();
            // 1. 默认情况下     为今天的各个店铺的出库
            if ((string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2))&&string.IsNullOrEmpty(waresName))
            {
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date;
                condition = x => x.orderStatus == 1 && x.orderTime <= todayNow && x.orderTime >= midnight;
                var orderList1 = bll.Search().Where(condition).Select(x => new { x.orderTime, x.storeid, x.orderCode, x.orderSumprice }).ToList();

                //1.遍历orderList   
                foreach (var oneOrderCode in orderList1)
                {
                    //2.根据订单编号 查询 订单详情表 相关记录信息  所有的记录  只是对一个订单所涉及到的商品进行分类

                    var orderdetailList = orderDetailBll.Search(x => x.orderCode == oneOrderCode.orderCode).Select(x => new { x.storeid, x.waresQuantity }).ToList();

                    foreach (var item in orderdetailList)
                    {
                        ListDetailall modelAllDetail = new ListDetailall();
                        modelAllDetail.saleNum = item.waresQuantity;
                        modelAllDetail.shopId = item.storeid;
                        modelAllDetail.orderSumprice = oneOrderCode.orderSumprice;
                        listAlldetail.Add(modelAllDetail);  ///这就是所有的商品的汇总 形成一个新的订单商品详情表
                    }
                }
                if (listAlldetail.Count != 0)
                {
                    var listdetailByshop = listAlldetail.GroupBy(x => x.shopId).Select(x => new { x.FirstOrDefault().shopId, saleNum = x.Sum(y => y.saleNum) }).ToList();
                    foreach (var item in listdetailByshop)
                    {
                        ListSaleDetail modelAllWare = new ListSaleDetail();
                        modelAllWare.shopName = storeBll.Search(x => x.storeid == item.shopId).Select(x => new { x.storeName }).ToList().FirstOrDefault().storeName;
                        modelAllWare.saleNum = item.saleNum;
                       // modelAllWare.saleMoney = orderList1.Where(x => x.storeid == item.shopId).GroupBy(x => x.storeid).Select(x => new { orderSumprice = x.Sum(y => y.orderSumprice) }).ToList().FirstOrDefault().orderSumprice;
                        //根据店铺分组查出相应的 营业金额
                        var salemoneyList = orderList1.Where(x => x.storeid == item.shopId).GroupBy(x => x.storeid).Select(x => new { orderSumprice = x.Sum(y => y.orderSumprice) }).ToList();
                        if (salemoneyList.Count != 0)
                        {
                            modelAllWare.saleMoney = salemoneyList.FirstOrDefault().orderSumprice;
                        }

                        else
                        {
                            modelAllWare.saleMoney = 0;
                        }
                        listSale.Add(modelAllWare);  
                    }
                }
            }
            //根据 时间段查询
             if (date1!=null&&date2!=null&&date1!=""&&date2!=""&&(waresName==""||waresName==""))
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
                var orderList = bll.Search().Where(condition).Select(x => new { x.orderTime, x.storeid, x.orderCode, x.orderSumprice }).ToList();

                //1.遍历orderList   
                foreach (var oneOrderCode in orderList)
                {
                    //2.根据订单编号 查询 订单详情表 相关记录信息  所有的记录  只是对一个订单所涉及到的商品进行分类

                    var orderdetailList = orderDetailBll.Search(x => x.orderCode == oneOrderCode.orderCode).Select(x => new { x.storeid,  x.waresQuantity }).ToList();

                    foreach (var item in orderdetailList)
                    {
                        ListDetailall modelAllDetail = new ListDetailall();
                        modelAllDetail.saleNum = item.waresQuantity;
                        modelAllDetail.shopId = item.storeid;
                        modelAllDetail.orderSumprice = oneOrderCode.orderSumprice;
                        listAlldetail.Add(modelAllDetail);  ///这就是所有的订单详情的汇总 形成一个新的订单商品详情表
                    }
                }
                if (listAlldetail.Count != 0)
                {
                    var listdetailByshop = listAlldetail.GroupBy(x => x.shopId).Select(x => new { x.FirstOrDefault().shopId, saleNum = x.Sum(y => y.saleNum) }).ToList();
                    foreach (var item in listdetailByshop)
                    {
                        ListSaleDetail modelAllWare = new ListSaleDetail();
                        modelAllWare.shopName = storeBll.Search(x => x.storeid == item.shopId).Select(x => new { x.storeName }).ToList().FirstOrDefault().storeName;
                        modelAllWare.saleNum = item.saleNum;
                        //根据店铺分组查出相应的 营业金额
                        var salemoneyList = orderList.Where(x => x.storeid == item.shopId).GroupBy(x => x.storeid).Select(x => new { orderSumprice = x.Sum(y => y.orderSumprice) }).ToList();
                        if (salemoneyList.Count != 0)
                        {
                            modelAllWare.saleMoney = salemoneyList.FirstOrDefault().orderSumprice;
                        }

                        else
                        {
                            modelAllWare.saleMoney = 0;
                        }
                        
                        listSale.Add(modelAllWare);  ///这就是所有的订单详情的汇总 形成一个新的订单详情表
                    }
                }
            }
             //3. 根据 时间段查询 和  商品名称查询 
             if (date1 != null && date2 != null && date1 != "" && date2 != "" && waresName != "" && waresName != null)
             {
                 DateTime datetime1 = DateTime.Parse(date1);
                 DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                 condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
                 var orderList = bll.Search().Where(condition).Select(x => new { x.orderTime, x.storeid, x.orderCode, x.orderSumprice }).ToList();

                 //1.遍历orderList   
                 foreach (var oneOrderCode in orderList)
                 {
                     //2.根据订单编号 查询 订单详情表 相关记录信息  所有的记录  只是对一个订单所涉及到的商品进行分类
                     var orderdetailList = orderDetailBll.Search(x => x.orderCode == oneOrderCode.orderCode).Select(x => new { x.storeid,x.waresCode, x.waresQuantity }).ToList();

                     foreach (var item in orderdetailList)
                     {
                         ListWare modelAllWare = new ListWare();
                         modelAllWare.waresCode = item.waresCode;
                         modelAllWare.saleNum = item.waresQuantity;
                         modelAllWare.storeid = storeBll.Search().Where(x => x.storeid == item.storeid).Select(x => new { x.storeid }).FirstOrDefault().storeid;
                         listWare.Add(modelAllWare);  ///这就是所有的商品的汇总 形成一个新的订单商品详情表
                     }
                 }
                 //3.遍历listWare   然后再对每一种商品进行统计
                 string  wareCode= waresBll.Search(x => x.waresName.Contains(waresName)).Select(x => new { x.waresCode ,x.waresPrice}).FirstOrDefault().waresCode;

                 decimal? warePrice = waresBll.Search(x => x.waresName.Contains(waresName)).Select(x => new { x.waresCode, x.waresPrice }).FirstOrDefault().waresPrice;
                 var wareOrderList = listWare.Where(x => x.waresCode == wareCode).GroupBy(x => x.storeid).Select(x => new { x.FirstOrDefault().waresCode, x.FirstOrDefault().storeid, saleNum = x.Sum(y => y.saleNum) }).ToList();
                 foreach (var item in wareOrderList)
                 {
                     ListSaleDetail modelAllWare = new ListSaleDetail();
                     modelAllWare.shopName = storeBll.Search(x => x.storeid == item.storeid).Select(x => new { x.storeName }).ToList().FirstOrDefault().storeName;
                     modelAllWare.saleNum = item.saleNum;
                     modelAllWare.saleMoney =Convert.ToDecimal( warePrice * item.saleNum);
                     listSale.Add(modelAllWare);
                 }
             }

            return Json(listSale, JsonRequestBehavior.AllowGet);
        }
    }
}
